<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class Common {
    
    var $CI;
    var $mail;
    private $variable;
    
    function __construct() {
        $this->CI =& get_instance(); 
    }
    /**
     * 
     * @public
     * @param $data = array() with format key/value
     * @param to, cc, bcc can string or array('email1', 'email2')
     * @return void
     */
    function ConfigEmail($data){
        $this->mail = array(
            'from'      => $data['from'], 
            'from_name' => $data['from_name'],
            'subject'   => $data['subject'], 
            'to'        => $data['to'],
            'cc'        => $data['cc'],
            'bcc'       => $data['bcc'],
            'reply'     => $data['reply'],
            'rname'     => $data['rname'],
            'message'   => $data['msg']
         );
     }
     function SendMailSMTP(){         
        $config['protocol']     = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '10';
        $config['smtp_user']    = 'email'; 
        $config['smtp_pass']    = 'password'; 
        $config['charset']      = 'utf-8';
        $config['newline']      = "\r\n";
        $config['mailtype']     = 'html'; 
        $config['validation']   = TRUE;       
        $this->CI->load->library('email', $config); 
        //$this->initialize($config);        
        $this->CI->email->from($this->mail['from'], $this->mail['from_name']);
        $this->CI->email->to($this->mail['to']);
        // optional
        !empty($this->CI->mail['cc'])    ? $this->CI->email->cc($this->mail['cc'])                                : '';
        !empty($this->CI->mail['bcc'])   ? $this->CI->email->bcc($this->mail['bcc'])                              : ''; 
        !empty($this->CI->mail['reply']) ? $this->CI->email->reply_to($this->mail['reply'], $this->mail['rname']) : '';
        // end optional
        $this->CI->email->subject($this->mail['subject']);
        $this->CI->email->set_alt_message();
        $this->CI->email->message($this->mail['message']);
        $send = $this->CI->email->send();
        return $send;
    }
    /**
     * 
     * @public
     * @return string
     * @param
     *  $chars is string will random
     *  $length is number of length characters return
     * 
     */
    function RandomStr($chars = '123456789qwertyuiopasdfghjklmnbvcxz', $length = 6){
        $len = (strlen($chars) - 1);
        $string = $chars{rand(0, $len)};
        for($i = 1; $i < $length; $i = strlen($string)){
            $r = $chars{rand(0, $len)};
            if ($r != $string{$i - 1})
                $string .= $r;
        }
        return $string;
    }
    function VNtoEN($str) {
        $str = trim(mb_strtolower($str, 'UTF-8')); 
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
        $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
        $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
        $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
        $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
        $str = preg_replace("/(đ|ð)/", 'd', $str);
        $str = str_replace(' ', '', $str);
		return $str;
	} 
    /**
     * 
     * @public
     * @param 
     *  $string is string wil cut
     *  $limit is number word will keep in above string
     * @return string
     * this is the function to cut a chunk of characters in the string, used to cut get a short text to describe
     * exp: $string = 'this is the function to cut a chunk of characters in the string, used to cut get a short text to describe';
     * $limit: number words you want to cut from the text string into
     * $string = cutString($string, 8)
     * return $string = 'this is the function to cut a chunk...';
     * 
     */
    function cutString($string, $limit) { 
        $str = '';
        $string = explode(' ', trim($string));
    	if(sizeof($string) <= $limit) return implode(' ', $string);
    	else{
    		for($i = 0; $i < $limit; $i++){
    			$str .= $string[$i].' ';
    		}
    		$str = trim($str);
    		$str = $str.'...';
    	}
    	return $str;
    }
    
    
/* =====================================================

    END CLASS
    
======================================================== */    
}